/*
 * Copyright (c) Facebook, Inc. and its affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */

package com.facebook.fresco.middleware;


import com.facebook.fresco.ui.common.ControllerListener2;
import ohos.agp.utils.Point;
import ohos.agp.utils.Rect;
import ohos.utils.net.Uri;

import java.util.HashMap;
import java.util.Map;

public class MiddlewareUtils {

  public static ControllerListener2.Extras obtainExtras(
      Map<String, Object> componentAttribution,
      Map<String, Object> shortcutAttribution,
      Map<String, Object> dataSourceExtras,
      Rect viewportDimensions,
      String scaleType,
      Point focusPoint,
      Map<String, Object> imageExtras,
      Object callerContext,
      Uri mainUri) {
    final ControllerListener2.Extras extras = new ControllerListener2.Extras();
    extras.view = new HashMap<>();

    extras.view.putAll(componentAttribution);

    if (viewportDimensions != null) {
      extras.view.put("viewport_width", viewportDimensions.getWidth());
      extras.view.put("viewport_height", viewportDimensions.getHeight());
    } else {
      extras.view.put("viewport_width", -1);
      extras.view.put("viewport_height", -1);
    }
    extras.view.put("scale_type", scaleType);
    if (focusPoint != null) {
      extras.view.put("focus_point_x", focusPoint.position[0]);
      extras.view.put("focus_point_y", focusPoint.position[1]);
    }

    extras.view.put("caller_context", callerContext);
    if (mainUri != null) extras.view.put("uri_main", mainUri);

    if (dataSourceExtras != null) {
      extras.pipe = dataSourceExtras;
      if (imageExtras != null) extras.pipe.putAll(imageExtras);
    } else {
      extras.pipe = imageExtras;
      extras.view.putAll(shortcutAttribution);
    }

    return extras;
  }
}
